/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package queenb.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import queenb.net.entitybean.N100;
import queenb.web.crm.store.V030Store;
import queenb.web.util.ResourcesProvider;

/**
 *
 * @author VANANH
 */
public class ReportController extends QueenbController{

    private int id; // FV050
    private int excel;

    public ReportController(){
         Object pv050 = getRequest().getParameter("id");

         if(pv050 != null){
                 id = Integer.parseInt(pv050.toString());
         }
        
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the excel
     */
    public int getExcel() throws IOException {
        //get data
        V030Store store = new V030Store();
        List<N100> list = new ArrayList<N100>();
        try{
             System.out.println("---aaaaaaaaa+++++++++++++" + id);
             list = store.listV030(getLogin().getModule(), getLogin().getCompanyNo(),id, 0,0, getLogin().getUserName());
             System.out.println("aaaaaaaaa size" + list.size());


            File file = new File("D:\\Facilio\\Import\\kapi.xls");
            POIFSFileSystem fs =
            new POIFSFileSystem(new FileInputStream(file));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            System.out.println("so luong sheet" +wb.getNumberOfSheets() + "id" + id + "size" + list.size() );
            HSSFSheet sheet = wb.getSheetAt(0);



            HSSFCellStyle styleleft = wb.createCellStyle();
            styleleft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            styleleft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            styleleft.setBorderRight(HSSFCellStyle.BORDER_THIN);
            styleleft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            styleleft.setLeftBorderColor(HSSFColor.BLACK.index);
            styleleft.setRightBorderColor(HSSFColor.BLACK.index);
            styleleft.setBottomBorderColor(HSSFColor.BLACK.index);
            styleleft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            styleleft.setWrapText(true);


            for(int i = 0; i < list.size(); i++){
                        HSSFRow row = sheet.createRow((short)i+7);

                        HSSFCell cell1 = row.createCell((short)0 );
                        cell1.setCellValue(list.get(i).getHoten());
                        cell1.setCellStyle(styleleft);

                        HSSFCell cell2 = row.createCell((short)1 );
                        cell2.setCellValue(list.get(i).getNv109());
                         cell2.setCellStyle(styleleft);

                        HSSFCell cell3 = row.createCell((short)2 );
                        cell3.setCellValue(list.get(i).getNv105());
                        cell3.setCellStyle(styleleft);

                        HSSFCell cell4 = row.createCell((short)3 );
                        cell4.setCellValue(list.get(i).getDt());
                        cell4.setCellStyle(styleleft);

                        HSSFCell cell5 = row.createCell((short)4 );
                        cell5.setCellValue(list.get(i).getNv112());
                        cell5.setCellStyle(styleleft);
            }

            File fileout = new File(ResourcesProvider.getReportExcel() + "report.xls");
            FileOutputStream fileOut = new FileOutputStream( fileout);
            wb.write(fileOut);
            fileOut.close();
            String s = getRequest().getRequestURL().toString();
            String[] arr = new String[20];
            arr = s.split("/");
            String url = "";
            if(arr.length > 3)
                url = arr[0] + "/"+arr[1] + "/"+arr[2]+"/" ;

            sendRedirect(url + "resources/excel/report.xls");

        }catch(Exception e){
                System.out.println("error" + e.getMessage());
        }
        return excel;
    }

    /**
     * @param excel the excel to set
     */
    public void setExcel(int excel) {
        this.excel = excel;
    }

}
